package com.chenwang.measure.model.bean;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.chenwang.measure.utils.MyLogger;

import java.util.ArrayList;
import java.util.List;

/**
 * @松 on 2019/8/15 08:47
 * 测量类
 */
public class Task implements Parcelable {
    private WorkBean.OverviewDOListBean workTask; //任务
    public List<Segment> mList = new ArrayList<>(); //任务段

    //将一对信号机转成一条
    public WorkBean.OverviewDOListBean getWork(){
        workTask.getDetailedDOs().clear();
        for (int i=0; i<mList.size();i++) {
            workTask.getDetailedDOs().add(mList.get(i).getStatr());
            workTask.getDetailedDOs().add(mList.get(i).getEnd());
        }
        List<WorkBean.OverviewDOListBean.DetailedDOsBean> list = new ArrayList<>();
        List<WorkBean.OverviewDOListBean.DetailedDOsBean> temp = new ArrayList<>();


        for (int i =0; i< workTask.getDetailedDOs().size()-1 ;i++){
            if (workTask.getDetailedDOs().get(i).getForm()==workTask.getDetailedDOs().get(i+1).getForm()){
                if ((workTask.getDetailedDOs().get(i).getSemaphoreName().equals(workTask.getDetailedDOs().get(i+1).getSemaphoreName()))&&
                        (workTask.getDetailedDOs().get(i).getRwmxId()==workTask.getDetailedDOs().get(i+1).getRwmxId())) {
                       if (TextUtils.isEmpty(workTask.getDetailedDOs().get(i+1).getImageUrl())){
                           workTask.getDetailedDOs().get(i+1).setImageUrl(workTask.getDetailedDOs().get(i).getImageUrl());
                       }
                 }else {
                    list.add( workTask.getDetailedDOs().get(i));
                    if (i == workTask.getDetailedDOs().size()-2){
                        workTask.getDetailedDOs().get(i+1).setSemaphoreSpace("");
                        list.add( workTask.getDetailedDOs().get(i+1));
                    }
                }

            }else {
                workTask.getDetailedDOs().get(i).setSemaphoreSpace("");
                list.add(workTask.getDetailedDOs().get(i));
            }
        }
       MyLogger.i(list.size() +"----------"+ workTask.getDetailedDOs().size());
        for (int i = 0 ; i <list.size() ;i++){
            MyLogger.i(list.get(i).getSemaphoreName() +"距离"+list.get(i).getSemaphoreSpace()+"标志"+list.get(i).getFlag());
        }
        workTask.setDetailedDOs(list);


        return workTask;
    }

    protected Task(Parcel in) {
        workTask = in.readParcelable(WorkBean.class.getClassLoader());
        mList = in.createTypedArrayList(Segment.CREATOR);
    }

    public static final Creator<Task> CREATOR = new Creator<Task>() {
        @Override
        public Task createFromParcel(Parcel in) {
            return new Task(in);
        }

        @Override
        public Task[] newArray(int size) {
            return new Task[size];
        }
    };

    public List<Segment> getList() {
        return mList;
    }

    public void setList(List<Segment> list) {
        mList = list;
    }

    public Task(WorkBean.OverviewDOListBean workTask) {
        this.workTask = workTask;
        initSeament();
    }

    public WorkBean.OverviewDOListBean getWorkTask() {
        return workTask;
    }

    public void setWorkTask(WorkBean.OverviewDOListBean workTask) {
        this.workTask = workTask;
    }

    // 将一条条信号机转成一对
    private void initSeament(){
        for (int i =0; i< workTask.getDetailedDOs().size()-1;i++){
            workTask.getDetailedDOs().get(i).setFlag(workTask.getDetailedDOs().get(i).getForm()+"");
          /*  List<Integer> list = new ArrayList<>();
            list.add(1);*/
             if(workTask.getDetailedDOs().get(i).getForm()==workTask.getDetailedDOs().get(i+1).getForm()){
                 workTask.getDetailedDOs().get(i+1).setFlag(workTask.getDetailedDOs().get(i+1).getForm()+"");
                 //workTask.getDetailedDOs().get(i).setGroupId(list);
                 //workTask.getDetailedDOs().get(i+1).setGroupId(list);
                // MyLogger.i(workTask.getDetailedDOs().get(i).getGroupId().size()+"分组id");
                 workTask.getDetailedDOs().get(i).setNext( workTask.getDetailedDOs().get(i).getSemaphore());
                 workTask.getDetailedDOs().get(i+1).setLast(workTask.getDetailedDOs().get(i).getSemaphore());
                 Segment segment = new Segment(workTask.getDetailedDOs().get(i), workTask.getDetailedDOs().get(i+1));
                 mList.add(segment);
             }
        }



    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeParcelable(workTask, flags);
        dest.writeTypedList(mList);
    }

    public static class Segment implements Parcelable{

        private boolean isSave; //是否保存
        private int lineType;// 1 直线 3折线  2曲线
        private int startEndFlag;// 0 开始信号机  1结束点信号机  标识
        private int count;//折线计数
        private int line1Count ;//直线
        private int line2Count ;//曲线
        private int line3Count ;//折线
        private double distance; //距离
        private Double latStart;//起始点纬度
        private Double lonStart;//起始点经度
        private Double latEnd;//结束点纬度
        private Double lonEnd;//结束点经度
        private LonLatBean lonLat; //上一次保存的经纬度 折线曲线

        private int state;// //点位是否测量过  0 未   1 开始  2结束  3点都测量过都是相同点 4完成
        private WorkBean.OverviewDOListBean.DetailedDOsBean statr;
        private WorkBean.OverviewDOListBean.DetailedDOsBean end;
        private int position;


        protected Segment(Parcel in) {
            isSave = in.readByte() != 0;
            lineType = in.readInt();
            startEndFlag = in.readInt();
            count = in.readInt();
            line1Count = in.readInt();
            line2Count = in.readInt();
            line3Count = in.readInt();
            distance = in.readDouble();
            if (in.readByte() == 0) {
                latStart = null;
            } else {
                latStart = in.readDouble();
            }
            if (in.readByte() == 0) {
                lonStart = null;
            } else {
                lonStart = in.readDouble();
            }
            if (in.readByte() == 0) {
                latEnd = null;
            } else {
                latEnd = in.readDouble();
            }
            if (in.readByte() == 0) {
                lonEnd = null;
            } else {
                lonEnd = in.readDouble();
            }
            lonLat = in.readParcelable(LonLatBean.class.getClassLoader());
            state = in.readInt();
            statr = in.readParcelable(WorkBean.OverviewDOListBean.DetailedDOsBean.class.getClassLoader());
            end = in.readParcelable(WorkBean.OverviewDOListBean.DetailedDOsBean.class.getClassLoader());
            position = in.readInt();
        }

        public static final Creator<Segment> CREATOR = new Creator<Segment>() {
            @Override
            public Segment createFromParcel(Parcel in) {
                return new Segment(in);
            }

            @Override
            public Segment[] newArray(int size) {
                return new Segment[size];
            }
        };

        public boolean isSave() {
            return isSave;
        }

        public void setSave(boolean save) {
            isSave = save;
        }

        public int getLineType() {
            return lineType;
        }

        public int getStartEndFlag() {
            return startEndFlag;
        }

        public void setStartEndFlag(int startEndFlag) {
            this.startEndFlag = startEndFlag;
        }

        public int getCount() {
            return count;
        }

        public void setCount(int count) {
            this.count = count;
        }

        public int getLine1Count() {
            return line1Count;
        }

        public void setLine1Count(int line1Count) {
            this.line1Count = line1Count;
        }

        public int getLine2Count() {
            return line2Count;
        }

        public void setLine2Count(int line2Count) {
            this.line2Count = line2Count;
        }

        public int getLine3Count() {
            return line3Count;
        }

        public void setLine3Count(int line3Count) {
            this.line3Count = line3Count;
        }

        public double getDistance() {
            return distance;
        }

        public Double getLatStart() {
            return latStart;
        }

        public void setLatStart(Double latStart) {
            this.latStart = latStart;
        }

        public Double getLonStart() {
            return lonStart;
        }

        public void setLonStart(Double lonStart) {
            this.lonStart = lonStart;
        }

        public Double getLatEnd() {
            return latEnd;
        }

        public void setLatEnd(Double latEnd) {
            this.latEnd = latEnd;
        }

        public Double getLonEnd() {
            return lonEnd;
        }

        public void setLonEnd(Double lonEnd) {
            this.lonEnd = lonEnd;
        }

        public LonLatBean getLonLat() {
            return lonLat;
        }

        public void setLonLat(LonLatBean lonLat) {
            this.lonLat = lonLat;
        }





        public int getPosition() {
            return position;
        }

        public void setPosition(int position) {
            this.position = position;
        }



        public Segment(WorkBean.OverviewDOListBean.DetailedDOsBean detailedDOsBean, WorkBean.OverviewDOListBean.DetailedDOsBean detailedDOsBean1) {
                      statr = detailedDOsBean;
                      end = detailedDOsBean1;
        }
        public int getState() {
            return state;
        }

        public void setState(int state) {
            this.state = state;
        }



        public WorkBean.OverviewDOListBean.DetailedDOsBean getStatr() {
            return statr;
        }

        public void setStatr(WorkBean.OverviewDOListBean.DetailedDOsBean statr) {
            this.statr = statr;
        }

        public WorkBean.OverviewDOListBean.DetailedDOsBean getEnd() {
            return end;
        }

        public void setEnd(WorkBean.OverviewDOListBean.DetailedDOsBean end) {
            this.end = end;
        }

        public void setLineType(int line){
            lineType =line;
            statr.setLineType(line+"");
            end.setLineType(line+"");

        }

        public void setDistance(double distance){
            this.distance = distance;
            statr.setSemaphoreSpace(distance+"");
            end.setSemaphoreSpace(distance+"");


        }

        //设置连续点
        public void setMultipoint(String multipoint){
            statr.setMultipoint(multipoint);
            end.setMultipoint(multipoint);
        }


        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel parcel, int i) {
            parcel.writeByte((byte) (isSave ? 1 : 0));
            parcel.writeInt(lineType);
            parcel.writeInt(startEndFlag);
            parcel.writeInt(count);
            parcel.writeInt(line1Count);
            parcel.writeInt(line2Count);
            parcel.writeInt(line3Count);
            parcel.writeDouble(distance);
            if (latStart == null) {
                parcel.writeByte((byte) 0);
            } else {
                parcel.writeByte((byte) 1);
                parcel.writeDouble(latStart);
            }
            if (lonStart == null) {
                parcel.writeByte((byte) 0);
            } else {
                parcel.writeByte((byte) 1);
                parcel.writeDouble(lonStart);
            }
            if (latEnd == null) {
                parcel.writeByte((byte) 0);
            } else {
                parcel.writeByte((byte) 1);
                parcel.writeDouble(latEnd);
            }
            if (lonEnd == null) {
                parcel.writeByte((byte) 0);
            } else {
                parcel.writeByte((byte) 1);
                parcel.writeDouble(lonEnd);
            }
            parcel.writeParcelable(lonLat, i);
            parcel.writeInt(state);
            parcel.writeParcelable(statr, i);
            parcel.writeParcelable(end, i);
            parcel.writeInt(position);
        }
    }

}
